import { httpClient } from '@/utils/http/axios';
import { ErrorMessageMode } from '#/axios';
import { AjaxResult } from '@/api/model';
import { GatewayClientDto, GatewayInputDto, GatewayOutputDto } from './model';
import { PageRequest, PageData } from '@/utils/common/types';

/** IoT网关枚举 */
enum Api {
  /** 读取网关客户端 */
  ReadClient = '/api/iot/admin/gateway/readClient',

  /** 读取网关 */
  Read = '/api/iot/admin/gateway/read',

  /** 新增网关 */
  Create = '/api/iot/admin/gateway/create',
  /** 更新网关 */
  Update = '/api/iot/admin/gateway/update',
  /** 删除网关 */
  Delete = '/api/iot/admin/gateway/delete',
}

//#region 读取网关客户端
/**
 * 读取网关客户端
 * @param request 分页请求
 * @param mode 错误消息模型
 */
export function readGatewayClient(request: PageRequest, mode: ErrorMessageMode = 'message') {
  return httpClient.post<PageData<GatewayClientDto>>(
    {
      url: Api.ReadClient,
      params: request,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 读取网关
/**
 * 读取网关
 * @param request 分页请求
 * @param mode 错误消息模型
 */
export function readGateway(request: PageRequest, mode: ErrorMessageMode = 'message') {
  return httpClient.post<PageData<GatewayOutputDto>>(
    {
      url: Api.Read,
      params: request,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

/**
 * 新增网关
 * @param dtos 网关DTO
 * @param mode 错误消息模型
 */
export function createGateway(dtos: GatewayInputDto[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Create,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 更新网关
 * @param dtos 网关DTO
 * @param mode 错误消息模型
 */
export function updateGateway(dtos: GatewayInputDto[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Update,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 删除网关
 * @param ids 网关编号
 * @param mode 错误消息模型
 */
export function deleteGateway(ids: (string | bigint)[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Delete,
      params: ids,
    },
    {
      errorMessageMode: mode,
    },
  );
}
